/**
 * 
 */
package com.maszy.commons.tools;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;

import com.maszy.bean.Breakfast;
import com.maszy.bean.Dept;
import com.maszy.bean.DoorStore;
import com.maszy.bean.GoodPlan;
import com.maszy.bean.Product;
import com.maszy.db.ConfigurableConstants;
import com.maszy.service.BreakfastService;
import com.maszy.service.DeptService;
import com.maszy.service.DoorStoreService;
import com.maszy.service.GoodPlanService;
import com.maszy.service.ProductService;

/**
 * @作者 Allen Shu
 * @创建日期 2012-6-19
 * @版本 V 1.0
 */

public class PrintGoodPlanReport3 implements Printable {
	private int PAGES = 0;
	/*
	 * Graphic指明打印的图形环境；PageFormat指明打印页格式（页面大小以点为计量单位，
	 * 1点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点）；page指明页号
	 */
	private final int _titleFontSize = 15;
	private final int _contentFontSize = 12;
	private final int _x = 20;
	private final int _x2 = 300;
	private final int _y = 20;
	private final int stepy = 10;
	String goodType = "";
	private GoodPlan goodplan;
	private String deptName="0";
	// 打印内容到指定位置
	public void printContent() {
		String printName =ConfigurableConstants.getProperty("breakfastPrint");
//		String printName ="print2";
		System.out.println("调用打印机:"+printName);
		PAGES = 1; // 获取打印总页数
		// 指定打印输出格式
		DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
		DocAttributeSet das = new HashDocAttributeSet();
		// 定位默认的打印服务
		// PrintService printService =
		// PrintServiceLookup.lookupDefaultPrintService();
	

		// printName=goodplan.getPrint_name();
		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(
				flavor, das);
		for (int i = 0; i < printServices.length; i++) {
//			 System.out.println("第"+(i+1)+"个打印机:"+printServices[i].getName()+"  printName:"+printName);
			if (printName.equals(printServices[i].getName())) {
				// 创建打印作业
				DocPrintJob job = printServices[i].createPrintJob();
				// 设置打印属性
				PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
				// 设置纸张大小,也可以新建MediaSize类来自定义大小
				pras.add(MediaSizeName.ISO_A4);

				// 指定打印内容
				Doc doc = new SimpleDoc(this, flavor, das);
				// 不显示打印对话框，直接进行打印工作
				try {
					job.print(doc, pras); // 进行每一页的具体打印操作
				} catch (PrintException pe) {
					pe.printStackTrace();
				}
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.print.Printable#print(java.awt.Graphics,
	 * java.awt.print.PageFormat, int)
	 */
	public int print(Graphics gp, PageFormat pf, int page)
			throws PrinterException {
		// TODO Auto-generated method stub
		Graphics2D g2 = (Graphics2D) gp;
		g2.setPaint(Color.black); // 设置打印颜色为黑色
		if (page >= PAGES) // 当打印页号大于需要打印的总页数时，打印工作结束
			return Printable.NO_SUCH_PAGE;
		g2.translate(pf.getImageableX(), pf.getImageableY());// 转换坐标，确定打印边界
		queryGoodPlanReportByDept(g2,20,50);
		return 0;
	}

	public void queryGoodPlanReportByDept(Graphics2D g2, int x, int y) {
		GoodPlan obj=new GoodPlan();
		String date=new SimpleDateFormat("yyyy-MM-dd").format(Calendar
				.getInstance().getTime());
		obj.setGoodPlan_BeginDate(date+" 00:00:00");
		obj.setGoodPlan_EndDate(date+" 23:59:59");
//		obj.setGoodPlan_BeginDate("2012-06-29"+" 00:00:00");
//		obj.setGoodPlan_EndDate("2012-06-29"+" 23:59:59");
		obj.setGoodPlan_Type("4");
		//先获取所有门店的信息
		DoorStoreService doorStoreService=new DoorStoreService();
		List<DoorStore> doorStoreList=doorStoreService.queryAll();
		//获取所有产品信息
		BreakfastService breakfastService=new BreakfastService();
		List<Breakfast>  breakfastList=breakfastService.queryAll();
		//查询当日所有门店的原材料供应单的报表情况.
		GoodPlanService goodPlanService=new GoodPlanService();
		List<GoodPlan> goodPlanList=goodPlanService.queryOneDayGoodPlan2(obj);
		//先循环出所有单品
		List<List<Object>> list=new ArrayList<List<Object>>();
		if(goodPlanList!=null){
			if(goodPlanList.size()>0){
				System.out.println("PrintGoodPlanReport3开始打印");
				g2.setFont(new Font("宋体", Font.PLAIN, _titleFontSize));
				g2.drawString("早点要货单" + "----"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar
						.getInstance().getTime()), _x + 150, _y); // 具体打印每一行文本，同时走纸移位
				g2.setFont(new Font("宋体", Font.PLAIN, 12));
				//按部门循环
				PrintReport(g2,x,y ,doorStoreList,breakfastList,goodPlanList);
			}else{
				System.out.println("goodPlanList.size=0,未打印");
			}
		}else{
			System.out.println("goodPlanList==null,未打印");
		}
		
		
	}
	public void PrintReport(Graphics2D g2, int x, int y,List<DoorStore> doorStoreList,List<Breakfast> breakfastList,List<GoodPlan> goodPlanList){
		List<List<Object>> list = new ArrayList<List<Object>>();
		g2.drawString("品名",x,y-5);
		int tX=x+55;
		g2.drawString("规格",tX,y-5);
		tX+=55;
		g2.drawString("时间/趟",tX,y-5);
		tX+=65;
		for(int z=1;z<doorStoreList.size();z++){
			g2.drawString(doorStoreList.get(z).getDoorStore_Name().substring(0,2),tX,y-5);
			tX+=35;
			if(z==doorStoreList.size()-1){
				g2.drawString("合计",tX,y-5);
			}
		}
		for(int i=0;i<breakfastList.size();i++){
			LabelA:
				//循环早晨趟数
				for(int j=1;j<=breakfastList.get(i).getMorning_tang();j++){
					//循环出所有要货计划
					int sum=0;
					List<Object> reportItemList=new ArrayList<Object>();
//					reportItemList.add("<td>"+breakfastList.get(i).getProduct_name()+"</td>");
//					reportItemList.add("<td>上午第"+j+"趟</td>");
					String printStr="";
					printStr=breakfastList.get(i).getProduct_name()+"|"+breakfastList.get(i).getSpecification()+"|"+"上午第"+j+"趟|";
					LabelB:
					for(int k=1;k<doorStoreList.size();k++){
						//循环出所有门店
						boolean b=false;
//						System.out.println("开始循环门店\t"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟"+"\t"+breakfastList.get(i).getProduct_name());
						LabelC:
						for(int m=0;m<goodPlanList.size();m++){
							if(goodPlanList.get(m).getDoorStore_Id().equals(doorStoreList.get(k).getDoorStore_Id())){
								//如果有该门店.循环这家门店要货计划明细数据
								List<Breakfast> planBodyList=goodPlanList.get(m).getBreakfastList();
								//循环具体要货内容
								for(int n=0;n<planBodyList.size();n++){
									//判断早晨第几次要货.是否匹配.
									if(planBodyList.get(n).getDept_name().equals(Integer.toString(j))&&"上午".equals(planBodyList.get(n).getRemark())&&planBodyList.get(n).getBreakfast_id()==breakfastList.get(i).getBreakfast_id()){
										printStr+=planBodyList.get(n).getQuantity()+"|";
										//reportItemList.add("<td>"+planBodyList.get(n).getQuantity()+"</td>");
										sum+=planBodyList.get(n).getQuantity();
										b=true;
//										System.out.println(doorStoreList.get(k).getDoorStore_Name()+"\t"+planBodyList.get(n).getRemark()+"第"+planBodyList.get(n).getDept_name()+"趟\t"+planBodyList.get(n).getProduct_name()+"\t"+planBodyList.get(n).getQuantity()+"\t 小计"+sum);
										break LabelC;
									}
								}
							}
						}
						if(b==false){
							printStr+=0+"|";
//							reportItemList.add("<td>0</td>");
	//						System.out.println("没匹配上"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟\t"+"\t"+breakfastList.get(i).getProduct_name()+"\t 0个"+"\t 小计"+sum);
						}
					}
					printStr+=sum;
					if(sum>0){
						y=y+stepy;
						int tempX=x;
						String[] print_arr=printStr.split("[|]");
						for(int q=0;q<print_arr.length;q++){
							if(q==1){
								tempX=tempX+20;
							}if(q==2){
								tempX=tempX+20;
							}if(q==3){
								tempX=tempX+35;
							}
							g2.drawString(print_arr[q], tempX, y);
							System.out.print(print_arr[q]+"\t");
							tempX=tempX+35;
						}
						System.out.println();
						System.out.println("--------------------------------------------------");
						y=y+10;
						if(j%2==0){
							g2.setFont(new Font("宋体", Font.PLAIN, 1));
							 	float lineWidth = 1.0f;
						      ((Graphics2D)g2).setStroke(new BasicStroke(lineWidth));
						      ((Graphics2D)g2).drawLine(_x, y-5, 530, y-5);
						}else{
							g2.setFont(new Font("宋体", Font.PLAIN, 6));
							g2.drawString("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------", _x, y);
						}
						g2.setFont(new Font("宋体", Font.PLAIN, 12));
					}
//					reportItemList.add("<td>"+sum+"</td>");
					System.out.println("第"+j+"趟匹配结束.加得总和为:"+sum);
//					System.out.println(reportItemList);
					list.add(reportItemList);
				}
		
		//下午的部分
		LabelA:
			//循环下午趟数
			for(int j=1;j<=breakfastList.get(i).getAfternoon_tang();j++){
				//循环出所有要货计划
				int sum=0;
				List<Object> reportItemList=new ArrayList<Object>();
//				reportItemList.add("<td>"+breakfastList.get(i).getProduct_name()+"</td>");
//				reportItemList.add("<td>下午第"+j+"趟</td>");
				String printStr="";
				printStr=breakfastList.get(i).getProduct_name()+"|"+breakfastList.get(i).getSpecification()+"|"+"下午第"+j+"趟|";
				LabelB:
				for(int k=1;k<doorStoreList.size();k++){
					//循环出所有门店
					boolean b=false;
//					System.out.println("开始循环门店\t"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟"+"\t"+breakfastList.get(i).getProduct_name());
					LabelC:
					for(int m=0;m<goodPlanList.size();m++){
						if(goodPlanList.get(m).getDoorStore_Id().equals(doorStoreList.get(k).getDoorStore_Id())){
							//如果有该门店.循环这家门店要货计划明细数据
							List<Breakfast> planBodyList=goodPlanList.get(m).getBreakfastList();
							//循环具体要货内容
							for(int n=0;n<planBodyList.size();n++){
								//判断下午第几次要货.是否匹配.
								if(planBodyList.get(n).getDept_name().equals(Integer.toString(j))&&"下午".equals(planBodyList.get(n).getRemark())&&planBodyList.get(n).getBreakfast_id()==breakfastList.get(i).getBreakfast_id()){
//									reportItemList.add("<td>"+planBodyList.get(n).getQuantity()+"</td>");
									printStr+=planBodyList.get(n).getQuantity()+"|";
									sum+=planBodyList.get(n).getQuantity();
									b=true;
//									System.out.println(doorStoreList.get(k).getDoorStore_Name()+"\t"+planBodyList.get(n).getRemark()+"第"+planBodyList.get(n).getDept_name()+"趟\t"+planBodyList.get(n).getProduct_name()+"\t"+planBodyList.get(n).getQuantity()+"\t 小计"+sum);
									break LabelC;
								}
							}
						}
					}
					if(b==false){
						printStr+=0+"|";
//						System.out.println("没匹配上"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟\t"+"\t"+breakfastList.get(i).getProduct_name()+"\t 0个"+"\t 小计"+sum);
					}
				}
//				reportItemList.add("<td>"+sum+"</td>");
//				System.out.println("第"+j+"趟匹配结束.加得总和为:"+sum);
//				System.out.println(reportItemList);
				printStr+=sum;
				if(sum>0){
					y=y+stepy;
					int tempX=x;
					String[] print_arr=printStr.split("[|]");
					for(int q=0;q<print_arr.length;q++){
						if(q==1){
							tempX=tempX+20;
						}if(q==2){
							tempX=tempX+20;
						}if(q==3){
							tempX=tempX+35;
						}
						g2.drawString(print_arr[q], tempX, y);
						System.out.print(print_arr[q]+"\t");
						tempX=tempX+35;
					}
					System.out.println();
					System.out.println("--------------------------------------------------");
					y=y+10;
					if(j%2==0){
						g2.setFont(new Font("宋体", Font.PLAIN, 1));
						 	float lineWidth = 1.0f;
					      ((Graphics2D)g2).setStroke(new BasicStroke(lineWidth));
					      ((Graphics2D)g2).drawLine(_x, y-5, 530, y-5);
					}else{
						g2.setFont(new Font("宋体", Font.PLAIN, 6));
						g2.drawString("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------", _x, y);
					}
					g2.setFont(new Font("宋体", Font.PLAIN, 12));
				}
				list.add(reportItemList);
			}
				y=y+20;
		}
	}
	public static void main(String args[]) throws FileNotFoundException,
			MalformedURLException {
		PrintGoodPlanReport3 ptool = new PrintGoodPlanReport3();
		ptool.printContent();
	}
}
